define(function (require) {
    "use strict";
    var ko = require("knockout");
    var komapping = require("komapping");
    var router = require("plugins/router");
    var layui = require("layui");
    var $ = require("jquery");
    var system = require("durandal/system");

    var TreeNodeStaeTemp = function () {
        var self = this;

        self.treeNodeIsOpenTemp = {};
        self.currentTId = null;

        self.saveIsOption = function (treeNode) {
            if ((typeof treeNode === "object") && (treeNode != null)) {
                var open = (treeNode.open === true) ? true : false;
                treeNode.open = open;
                self.treeNodeIsOpenTemp[treeNode.id.toString()] = open;
                return open;
            }
        };
        self.resetFromTemp = function (data) {
            var tempIsOpen = (self.treeNodeIsOpenTemp[data.id.toString()] === true) ? true : false;
            data.open = tempIsOpen;
            for (var i = 0; i < data.children.length; i++) {
                treeNodeStaeTemp.resetFromTemp(data.children[i]);
            }
            return data;
        };

        self.saveCurrent = function (treeNode) {
            self.currentTId = treeNode.tId;
        };
        self.selectTempNode = function () {
            var treeObj = $.fn.zTree.getZTreeObj("tree-list");
            if (self.currentTId !== null) {
                var currentNode = treeObj.getNodeByTId(self.currentTId);
                if (currentNode !== null) {
                    treeObj.selectNode(currentNode);
                }
            }
        };
        /*self.findItemById = function (id, currentNodes) {
            for (var i = 0; i < currentNodes.length; i++) {
                var node = currentNodes[i];
                if (node.id === id) {
                    return node;
                }
                var childrenResult = self.findItemById(id, node.children);
                if (childrenResult !== null) {
                    return childrenResult;
                }
            }
            return null;
        };*/
    };

    var treeNodeStaeTemp = new TreeNodeStaeTemp();

    return {
        compositionComplete: function () {
            layui.use(["element", "form", "layer", "table"], function () {
                var layer = layui.layer;
                var element = layui.element;
                var table = layui.table;
                var form = layui.form;
                element.render();
                form.render();

                var addHoverDom = function (treeId, treeNode) {
                    var sObj = $("#" + treeNode.tId + "_span");
                    if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
                    var addStr = "<span title='新建该节点及所有子节点的监控任务' class='button add' id='addBtn_" + treeNode.tId
                        + "' onfocus='this.blur();'></span>";
                    sObj.after(addStr);
                    var btn = $("#addBtn_" + treeNode.tId);
                    if (btn) btn.click(function () {
                        $.ajax({
                            type: "get",
                            url: "/back/callCrawl/" + treeNode.id,
                            dataType: "json"
                        }).then(function (response) {
                            layer.alert(response.msg);
                        }, function (data) {
                            layer.alert(data.responseJSON.msg);
                        });
                        return false;
                    });
                };

                var removeHoverDom = function (treeId, treeNode) {
                    $("#addBtn_" + treeNode.tId).unbind().remove();
                };

                var zTreeOnExpand = function (event, treeId, treeNode) {
                    treeNodeStaeTemp.saveIsOption(treeNode);
                };

                var zTreeOnCollapse = function (event, treeId, treeNode) {
                    treeNodeStaeTemp.saveIsOption(treeNode);
                };

                /*function updateNodeInfoByTreeId(treeIdObj) {
                    var treeId = parseInt(treeIdObj);
                    if ((typeof treeId === "number") && (treeId > 0)) {
                        $.ajax({
                            type: "get",
                            url: "/back/nodeInfo/" + encodeURIComponent(treeId),
                            dataType: "json"
                        }).then(function (response) {
                            nodeModel(komapping.fromJS(response.data));
                            form.render();
                        }, function (data) {
                            layer.alert(data.responseJSON.msg);
                        });
                    } else {
                        nodeModel(null);
                        form.render();
                    }
                };*/

                var zTreeOnClick = function (event, treeId, treeNode) {
                    if ((typeof treeNode === "object") && (treeNode !== null)) {
                        treeNodeStaeTemp.saveCurrent(treeNode);
                    } else {
                    }
                };

                var setting = {
                    view: {
                        addHoverDom: addHoverDom,
                        removeHoverDom: removeHoverDom,
                        selectedMulti: false
                    },
                    edit: {
                        enabled: false,
                        drag: {
                            isCopy: false,
                            isMove: false
                        }
                    },
                    callback: {
                        onExpand: zTreeOnExpand,
                        onCollapse: zTreeOnCollapse,
                        onClick: zTreeOnClick
                    },
                    data: {
                        simpleData: {
                            enable: false
                        }
                    }
                };

                var refreshTree = function () {
                    return $.ajax({
                        type: "get",
                        url: "/back/nodes",
                        dataType: "json"
                    }).then(function (response) {
                        $(function () {
                            $.fn.zTree.init($("#tree-list"), setting, treeNodeStaeTemp.resetFromTemp(response.data));
                            treeNodeStaeTemp.selectTempNode();
                        });
                    }, function (data) {
                        layer.alert(data.responseJSON.msg);
                    });
                };

                refreshTree();
            });
        }
    };
});